import router from './router'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title'
import { hasLogin } from '@/utils/auth'
// console.log(admin, normal)
NProgress.configure({ showSpinner: false, speed: 600 }) // NProgress Configuration
const whiteList = ['/login']
router.beforeEach(async (to, from, next) => {

  NProgress.start()
  document.title = getPageTitle(to.meta.title)
  const hasToken = hasLogin()
  if (hasToken) {
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next({ path: '/' })
      NProgress.done()
    } else {
      next()
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
